package com.bjb.vr.operation.controller;

import com.bjb.vr.common.ao.ResDictionaryAo;
import com.bjb.vr.common.ao.SysTenantAo;
import com.bjb.vr.common.constant.ThreadLocalConstant;
import com.bjb.vr.common.dto.UmsAccount;
import com.bjb.vr.common.result.BaseErrorCode;
import com.bjb.vr.common.result.CommonResult;
import com.bjb.vr.common.service.OptAreaService;
import com.bjb.vr.common.service.OptCustomerService;
import com.bjb.vr.common.service.ResDictionaryService;
import com.bjb.vr.common.service.UmsAccountService;
import com.bjb.vr.common.utils.ThreadLocalUtil;
import com.bjb.vr.common.vo.OptAreaVo;
import com.bjb.vr.common.vo.ResDictionaryVo;
import com.bjb.vr.common.vo.SysTenantVo;
import io.jsonwebtoken.lang.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * @description: 客户管理模块
 * @author: hujingbo
 * @create: 2022-08-08 15:28
 **/
@RestController
@RequestMapping("/api/operation/v1")
public class SysCustomerController {

    @Resource
    private OptCustomerService optCustomerService;

    @Resource
    private ResDictionaryService dictionaryService;

    @Resource
    private OptAreaService optAreaService;

    @Resource
    private UmsAccountService accountService;

    /**
     * 查询客户地址--关联查询
     *
     * @param tenantAo
     * @return
     */
    @PostMapping("/queryAddress")
    public CommonResult queryAddress(@Valid @RequestBody SysTenantAo tenantAo) {
        List<OptAreaVo> optAreaVos = optAreaService.queryAddress(tenantAo);
        return CommonResult.Ok().setResult(optAreaVos);
    }

    /**
     * 查询客户类别
     *
     * @return
     */
    @PostMapping("/queryCustomerType")
    public CommonResult queryCustomersType() {
        ResDictionaryAo dictionaryAo = new ResDictionaryAo();
        dictionaryAo.setDictType("10");
        List<ResDictionaryVo> dictionaryVos = dictionaryService.queryDictionaryByPeriodLink(dictionaryAo);
        return CommonResult.Ok().setResult(dictionaryVos);
    }

    /**
     * 查询客户信息--层级树
     *
     * @param tenantAo
     * @return
     */
    @PostMapping("/queryCustomersTree")
    public CommonResult queryCustomersTree(@Valid @RequestBody SysTenantAo tenantAo) {
        List<SysTenantVo> tenantVos = optCustomerService.queryCustomersTree(tenantAo);
        return CommonResult.Ok().setResult(tenantVos);
    }

    /**
     * 查询客户信息--列表
     *
     * @param tenantAo
     * @return
     */
    @PostMapping("/queryCustomersList")
    public CommonResult queryCustomersList(@Valid @RequestBody SysTenantAo tenantAo) {
        List<SysTenantVo> tenantVos = optCustomerService.queryCustomersList(tenantAo);
        return CommonResult.Ok().setResult(tenantVos);
    }

    /**
     * 新增客户信息
     *
     * @param tenantAo
     * @return
     */
    @PostMapping("/addCustomer")
    public CommonResult addCustomers(@Valid @RequestBody SysTenantAo tenantAo) {
        ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, true);
        // 查询账号是否存在
        UmsAccount account = accountService.getAccountByUsername(tenantAo.getMobile());
        if (Objects.nonNull(account)) {
            return CommonResult.Fail("用户账号已存在!");
        }
        // 新增租户
        boolean success = optCustomerService.addCustomer(tenantAo);
        if (!success) {
            return CommonResult.Fail(BaseErrorCode.ERROR_ADD);
        }
        return CommonResult.Ok();
    }

    /**
     * 查询客户信息--单条
     *
     * @param tenantAo
     * @return
     */
    @PostMapping("/queryCustomer")
    public CommonResult queryCustomer(@Valid @RequestBody SysTenantAo tenantAo) {
        Assert.notNull(tenantAo.getId(), "主键ID不能为空");
        SysTenantVo sysTenantVo = optCustomerService.queryCustomer(tenantAo);
        return CommonResult.Ok().setResult(sysTenantVo);
    }

    /**
     * 修改客户信息
     *
     * @param tenantAo
     * @return
     */
    @PostMapping("/updateCustomer")
    public CommonResult updateCustomer(@Valid @RequestBody SysTenantAo tenantAo) {
        Assert.notNull(tenantAo.getId(), "主键ID不能为空");
        boolean success = optCustomerService.updateCustomer(tenantAo);
        if (!success) {
            return CommonResult.Fail(BaseErrorCode.ERROR_ADD);
        }
        return CommonResult.Ok();
    }

    /**
     * 变更管理员
     *
     * @param tenantAo
     * @return
     */
    @PostMapping("/changeAdmin")
    public CommonResult changeAdmin(@Valid @RequestBody SysTenantAo tenantAo) {
        Assert.notNull(tenantAo.getId(), "主键ID不能为空");
        Assert.notNull(tenantAo.getMobile(), "客户账号不能为空");
        // 变更数据
        boolean success = optCustomerService.changeAdmin(tenantAo);
        if (!success) {
            return CommonResult.Fail(BaseErrorCode.ERROR_UPDATE);
        }
        return CommonResult.Ok();
    }
}
